/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/01/16 by CL $
*/
$(function(){
	
	//选择类型
	$(document).on("click", "[f='but_ser_type']", function(){
		$_id = $(this).attr("data-id");
		$("#s_type").val($_id);
		$("[f='but_ser_type']").removeClass("current");
		$(this).addClass("current");
		$("#dynamic_form").submit();
	});

	//搜索选择器
	$(document).on("click", "[f='but_selser']", function(){
		$_dd = $(this).next("dd");
		if ($_dd.css("display") == "none") {
			$(this).parent().addClass("current");
			$_dd.show();
		}
		else {
			$(this).parent().removeClass("current");
			$_dd.hide();
		}
	});
	
	//点击空白出隐藏选择器
	$(document).on("click", function(e){
		var target1  = $(e.target);
        if(target1.closest(".chose dl").length == 0){
			$(".chose dl dd").hide();
			$(".chose dl").removeClass("current");
		}
	});

	//下拉选1项 change
	$(document).on("change", "[f='but_sel_one']", function(){
		$_val = $(this).find("option:selected").val();
		$_text = $(this).find("option:selected").text();
		$_tips_text = $(this).attr("data-tips");
		if (typeof($_tips_text) == "undefined") {
			$_tips_text = "不限";
		}
		if ($_text == "=不限=") {
			$_text = $_tips_text;
		}
		$(this).parent().prev().children("em").html($_text);
		$(this).parent().hide();
		$(this).parent().parent().removeClass("current");
	});

	//范围确定选择2项
	$(document).on("click", "[f='but_selrange_confirm']", function(){
	
		$_data_1 = $(this).attr("data-1");
		$_data_2 = $(this).attr("data-2");
		$_data_unit = $(this).attr("data-unit");
		$_data_tips = $(this).attr("data-tips");
		if (typeof($_data_unit) == "undefined") {
			$_data_unit = "";
		}
		if (typeof($_data_tips) == "undefined") {
			$_data_tips = "";
		}
		$_val1 = $("#"+$_data_1).find("option:selected").val();
		$_text1 = $("#"+$_data_1).find("option:selected").text();
		$_val2 = $("#"+$_data_2).find("option:selected").val();
		$_text2 = $("#"+$_data_2).find("option:selected").text();

		if ($_val1 > 0 && $_val2 > 0) {
			$_text = $_text1+$_data_unit+"-"+$_text2+$_data_unit;
		}
		else {
			$_text = $_data_tips;
		}
		$(this).parent().prev().children("em").html($_text);
		$(this).parent().hide();
		$(this).parent().parent().removeClass("current");

	});

	//搜索按钮
	$(document).on("click", "[f='but_confirm_dynamicser']", function(){
		$("#dynamic_form").submit();
	});


	//保存评论
	$(document).on("click", "[f='but_save_comment']", function(){
		$_id = $(this).attr("data-id");
		$content = $("#comm_content").val();
		if ($_id.length == 0) {
			ToastShow("ID错误");
			return false;
		}
		if ($content.length == 0) {
			ToastShow("评论内容不能为空");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=dynamic",
			cache: false,
			data: {a:"savecomm", dyid:$_id, content:$content, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					$("#comm_content").val("");
					ToastShow("评论成功");
					_getCommPageList(1);
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("提交失败");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});



	//查看图片
	$(document).on("click", "[f='but_open_dynamic_zoom']", function(){
		$_id = $(this).attr("data-id"); //当前marry id
		$_orderi = $(this).attr("data-orderi"); //当前图片排序

		$("body").append("<div class='page_loading' style='display:block;' id='ajax_loading'><span></span></div>");
		$("html").addClass("dialog_lock_hide");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?c=dynamic",
			cache: false,
			data: {a:"zoom", id:$_id, orderi:$_orderi, datatype:"json", r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					$("#dynamic_zoom_data").html("");
					$("#dynamic_zoom_data").html($result);
					$("#dynamic_zoom_box").fadeIn(100, function(){
						$("html").addClass("dialog_lock_hide");
					});
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("获取数据失败");
					}
				}
				$("#ajax_loading").remove();
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});
	});

	//关闭图片
	$(document).on("click", "[f='but_close_dynamic_zoom']", function(){
		$("#dynamic_zoom_box").fadeOut(100, function(){
			$("#dynamic_zoom_data").html("");
			$("html").removeClass("dialog_lock_hide");
		});
	});

	//图片 上一张
	$(document).on("click", "[f='but_prev_zomm']", function(){
		$zoom_nums = parseInt($("#zoom_nums").val()); //张数
		$orderi = parseInt($("#orderi").val()); //当前
		$prev_orderi = 0;
		if ($orderi > 1) {
			$prev_orderi = ($orderi-1);
		}
		else {
			$prev_orderi = $zoom_nums;
		}		
		if ($prev_orderi > 0) {
			$("#zoom_orderi").html($prev_orderi);
			$("#orderi").val($prev_orderi);
			
			$("#zoom_list ._list").hide();
			$("#zoom_"+$prev_orderi).show();
		}

	});
	
	//图片 下一张
	$(document).on("click", "[f='but_next_zomm']", function(){
		$zoom_nums = parseInt($("#zoom_nums").val()); //张数
		$orderi = parseInt($("#orderi").val()); //当前
		$next_orderi = 0;
		if ($orderi < $zoom_nums) {
			$next_orderi = ($orderi+1);
		}
		else {
			$next_orderi = 1;
		}
		if ($next_orderi > 0) {
			$("#zoom_orderi").html($next_orderi);
			$("#orderi").val($next_orderi);

			$("#zoom_list ._list").hide();
			$("#zoom_"+$next_orderi).show();
		}

	});


	//显示隐藏操作项
	$(document).on("click", "[f='but_dynamic_showhand']", function(){
		if ($(this).parent().find("div").css("display") == "none") {
			$(this).parent().find("div").slideDown(200);
		}
		else {
			$(this).parent().find("div").slideUp(200);
		}
	});
	//删除
	$(document).on("click", "[f='but_dynamic_del']", function(){
		$_id = $(this).attr("data-id");
		$_from = $(this).attr("data-from");
		if (typeof($_from) == "undefined") {
			$_from = "list";
		}

		$_url = _ROOT_PATH+"index.php?c=dynamic&a=del&from="+$_from+"&id="+$_id;
		oeAjaxConfirmBox($_url, "确定要删除吗？", function(){
			if ($_from == "list") {
				window.location.reload();
			}
			else {
				
			}
		});
	});

	//发布动态 JSON
	$(document).on("click", "[f='but_open_adddynamic']", function(){
		if (_LOGIN_STATUS == 0) {
			_jdLogin();
			return false;
		}
		$_url = _ROOT_PATH+"index.php?c=dynamic&a=add&r="+get_rndnum(8);
		oeShadeBox("pop_add_dynamic", "ajax", $_url, 600, 450, function(){
			new Swiper('.swiper-container', {
				pagination: '.swiper-pagination',
				paginationClickable: true
			});
		});
	});
	//切换表情
	$(document).on("click", "[f='tab_em']", function(){
		if ($("#msg_face_box").css("display") == "block") {
			$("#msg_face_box").hide();
		}
		else {
			$("#msg_face_box").show();
			new Swiper('.swiper-container', {
				pagination: '.swiper-pagination',
				paginationClickable: true
			});
		}
	});
	//选择表情
	$(document).on("click", "[f='selectem']", function(){
		$_id = $(this).attr("data-id");
		$tag_em = "{emf_"+$_id+"}";
		var myField = document.getElementById("dynamic_content");
		if (document.selection) {
			//myField.focus();
			sel = document.selection.createRange();
			sel.text = $tag_em;
			//myField.focus();
		}
		else if (myField.selectionStart || myField.selectionStart == '0') {
			if (myField.value == "写点什么吧...") {
				myField.value = "";
			}
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			var cursorPos = endPos;
			myField.value = myField.value.substring(0, startPos)
						  + $tag_em
						  + myField.value.substring(endPos, myField.value.length);
			cursorPos += $tag_em.length;
			//myField.focus();
			myField.selectionStart = cursorPos;
			myField.selectionEnd = cursorPos;
		}
		else {
			if (myField.value == "写点什么吧...") {
				myField.value = "";
			}
			myField.value += $tag_em;
			//myField.focus();
		}
	});
	//保存发布
	$(document).on("click", "[f='but_save_adddynamic']", function(){
		$content = $("#dynamic_content").val();
		$thumbimg = $("#dynamic_thumbimg").val();
		$drawimg = $("#dynamic_drawimg").val();
		if ($content == "写点什么吧...") {
			$content = "";
		}
		if ($content.length == 0) {
			ToastShow("请填写动态内容");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?c=dynamic",
			cache: false,
			data: {
				content:$content, thumbimg:$thumbimg, drawimg:$drawimg,
				a:"saveadd", r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("发布成功");
					//关闭页面
					_closeShadeBox("pop_add_dynamic", function(){
						window.location.reload();
					});
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("发布失败");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});
	//动态上传图片
	$(document).on("change", "#file_dynamic_img", function(){
		$("#msg_face_box").hide();
		$("#dynamic_img_box").show();
		//上传图片
		$.ajaxFileUpload({
			"url": _ROOT_PATH+"index.php?m=user&c=index&a=upload&module=upload&thumbfiles=1",
			"secureuri": false,
			"fileElementId": "file_dynamic_img",
			"dataType": "json",
			"data": {uploadpart: "file_dynamic_img"},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$imgthumb = $json.thumbimg;
				$imgurl = $json.uploadimg;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") { //上传成功
					$("#dynamic_img_box").find("label").html("<img src='"+_ROOT_PATH+$imgurl+"' />").removeClass("loading");
					$("#dynamic_thumbimg").val($imgthumb);
					$("#dynamic_drawimg").val($imgurl);
					_oeloveFileSynYun($attid); //同步云存储
				}
				//失败
				else {
					$error = "上传失败，请检查图片大小和格式！";
					if ($result.length > 0 ) {
						$error = $result;
					}
					ToastShow($error);
					$("#dynamic_img_box").hide();
				}

			},
			error: function ($data, e, b) {
				ToastShow("上传失败，请检查图片大小和格式！");
			}
		});
	});
	
	//内容 JSON
	$(document).on("blur", "#dynamic_content", function(){
		if ($(this).val() == "写点什么吧...") {
			$(this).val("");
		}
	});
	$(document).on("click", "#dynamic_content", function(){
		if ($(this).val() == "写点什么吧...") {
			$(this).val("");
		}
	});


});

//ajax加载评论分页
function _getCommPageList($page) {
	if (typeof($page) == "undefined") {
		$page = 1;
	}
	$obj_box = $("#comm_json_data");
	$.ajax({
		type: "GET",
		url:  _ROOT_PATH+"index.php?c=dynamic&a=comm&datatype=json&dyid="+$("#dyid").val(),
		cache: false,
		data: {page:$page, r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$prepage = $json.prepage; //上一页
			$nextpage = $json.nextpage; //下一页
			$result = $json.result;
			if ($result.length > 0) {
				if ($page == "1") {
					$obj_box.html($result);
				}
				else {
					$obj_box.append($result);
				}
				if ($nextpage > 0) { //还有下一页
					$("[f='but_get_commpage']").attr("data-page", $nextpage);
					$("[f='but_get_commpage']").show();
					$("[f='but_get_commpage']").find("label").hide();
				}
				else { 
					$("[f='but_get_commpage']").hide();
				}
			}
		},
		error: function() {
		
		}
	});
}
